import json
import re
import os
import datetime
import collect
from openpyxl import Workbook
from openpyxl.compat import range
def test():
    f = open("reader_number", 'r', encoding='utf-8')
    list1 = []
    list2 = []
    for line in f.readlines():
        line = re.sub('\'', '\"', line)
        dic = json.loads(line)
        for key, value in dic.items():
            list1.append(key)
            list2.append(value)
    f.close()
    wb = Workbook()
    dest_filename = '{0}阅读数据表格.xlsx'.format(datetime.date.today())
    ws1 = wb.active
    ws1.title = "reading"
    ws1.column_dimensions['A'].width = 20.0
    ws1.column_dimensions['B'].width = 16.0
    for row in range(2, len(list1)+2):
        ws1['A1'] = u'账号'
        ws1['B1'] = u'阅读数'
        ws1['A{0}'.format(row)] = list1[row-2]
        if list2[row-2] != ''and list2[row-2] != 'None' and list2[row-2] != "一键登录异常":
            ws1['B{0}'.format(row)] = int(list2[row-2])
        else:
            ws1['B{0}'.format(row)] = list2[row - 2]
    wb.save(filename=dest_filename)
    os.remove('reader_number')
    
if __name__ == '__main__':
    collect.test_collect()
    test()